package com.lions.client.view.gui;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.lions.client.view.audio.AudioOutput;
import com.lions.client.view.popup.MyPopupPanel;

/**
 * Comments and things.
 * @author Eric
 */
public class LoginPanel extends DockPanel {

	/**
	 * Constructor for panel.
	 * @param h loginHandler
	 */
	public LoginPanel(LoginHandler h) {
		super();
		handler = h;

		passBox.setSize(hundred, hundred);
		userBox.setSize(hundred, hundred);

		
		setHorizontalAlignment(DockPanel.ALIGN_CENTER);
		addStyleName(dockCSS);
		
		Label title = new Label(dmhcs);
		title.setStyleName(titleLabelCSS);
		AbsolutePanel titlePanel = new AbsolutePanel();
		titlePanel.setStyleName(shortCSS);
		titlePanel.add(title);
		
		
		Label note = new Label(healthCare);
		note.setStyleName(noteLabelCSS);
		AbsolutePanel notePanel = new AbsolutePanel();
		notePanel.setStyleName(smallerCSS);
		notePanel.add(note);
		
		blankPanel = new AbsolutePanel();
		blankPanel.setStyleName(dockCSS);
		
		loginPanel.setWidget(0, 0, userLabel);
		loginPanel.setWidget(0, 1, userBox);
		loginPanel.setWidget(0, 2, userStatus);

		loginPanel.setWidget(1, 0, passLabel);
		loginPanel.setWidget(1, 1, passBox);
		loginPanel.setWidget(1, 2, passStatus);
		
		userLabel.addStyleName("defaultcursor");
		passLabel.addStyleName("defaultcursor");

		loginPanel.setWidget(2, 0, submit);
		submit.setStyleName(buttonCSS);
		userStatus.setVisible(false);
		passStatus.setVisible(false);

		
		
		blankFiller = new AbsolutePanel();
		blankFiller.setStyleName(shortCSS);
		
		add(blankFiller, DockPanel.NORTH);
		add(titlePanel, DockPanel.NORTH);
		add(notePanel, DockPanel.NORTH);
		add(loginPanel, DockPanel.CENTER);
		add(blankPanel, DockPanel.SOUTH);
		this.setVisible(true);
		createHandlers();
	}

	/**
	 * Gets the status of the login.
	 * @return bool status
	 */
	public boolean getStatus() {
		return status;
	}

	/**
	 * Sets the status of the login.
	 * @param s bool status
	 */
	private void setStatus(boolean s) {
		status = s;
	}

	/**
	 * Creates the click handlers.
	 */
	private void createHandlers() {
		submit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent e) {
				if ((userBox.getText().toUpperCase() == null)
						&& (passBox.getText().toUpperCase() == null)) {
					incorrectInfo();
				} else if (userBox.getText().toUpperCase().equals("USER1")) {
					successfulLogin();
				} else if (userBox.getText().toUpperCase().equals("ERIC")) {
					if (passBox.getText().toUpperCase().equals("WELLMAN")) {
						setStatus(true);
						setVisible(false);
					} else {
						incorrectPassword();
					}
				} else if (userBox.getText().toUpperCase().equals("BRAD")) {
					if (passBox.getText().toUpperCase().equals("CUTSHALL")) {
						successfulLogin();
					} else {
						incorrectPassword();
					}
				} else if (userBox.getText().toUpperCase().equals("KYLE")) {
					if (passBox.getText().toUpperCase().equals("FINK")) {
						successfulLogin();
					} else {
						incorrectPassword();
					}
				} else if (userBox.getText().toUpperCase().equals("TONY")) {
					if (passBox.getText().toUpperCase().equals("MENGELKOCH")) {
						successfulLogin();
					} else {
						incorrectPassword();
					}
				} else {
					incorrectUser();
				}
			}
		});
		
		submit.addKeyUpHandler(new KeyUpHandler() {
			
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_END) {
					new AudioOutput().playReadySong();
				}
			}
		});
	}
	
	/**
	 * Alerts the user on wront items.
	 */
	private void incorrectInfo() {
		new MyPopupPanel("Incorrect Info", "Username or Password is Incorrect");
		new AudioOutput().playInputError();
	}
	
	/**
	 * Alerts the user on failed login with wrong un.
	 */
	private void incorrectUser() {
		//userStatus.setText("Username invalid");
		new MyPopupPanel("Username Invalid","Username Invalid");
		new AudioOutput().playInputError();
	}
	
	/**
	 * Alerts the user on failed login with wrong pw.
	 */
	private void incorrectPassword() {
		new MyPopupPanel("Incorrect Password","Incorrect Password");
		new AudioOutput().playInputError();
	}
	
	/**
	 * Successful login stuff.
	 */
	private void successfulLogin() {
		setStatus(true);
		handler.loginSuccess();
	}

	/**
	 * Comment.
	 */
	private LoginHandler handler;
	/**
	 * Comment.
	 */
	private FlexTable loginPanel = new FlexTable();
	/**
	 * Comment.
	 */
	private final Button submit = new Button("Submit");
	/**
	 * Comment.
	 */
	private TextBox userBox = new TextBox();
	/**
	 * Comment.
	 */
	private PasswordTextBox passBox = new PasswordTextBox();
	/**
	 * Comment.
	 */
	private Label userLabel = new Label("Username:");
	/**
	 * Comment.
	 */
	private Label passLabel = new Label("Password:");
	/**
	 * Comment.
	 */
	private Label userStatus = new Label("");
	/**
	 * Comment.
	 */
	private Label passStatus = new Label("");
	/**
	 * Comment.
	 */
	private boolean status = false;
	/**
	 * Blank filler panel.
	 */
	private AbsolutePanel blankPanel;
	/**
	 * Another blank panel.
	 */
	private AbsolutePanel blankFiller;
	/**
	 * String.
	 */
	private String dmhcs = "DMHCS";
	/**
	 * String.
	 */
	private String healthCare = "At least it's not Healthcare.gov";
	/**
	 * CSS String.
	 */
	private String smallerCSS = "smallerdockpanel";
	/**
	 * CSS String.
	 */
	private String shortCSS = "shortdockpanel";
	/**
	 * CSS String.
	 */
	private String dockCSS = "dockpanel";
	/**
	 * CSS String.
	 */
	private String buttonCSS = "buttonstyle";
	/**
	 * CSS String.
	 */
	private String titleLabelCSS = "titlestyle";
	/**
	 * CSS String.
	 */
	private String noteLabelCSS = "notetext";
	/**
	 * CSS String.
	 */
	private String hundred = "100%";
}